//
//  CarTraderChoose.m
//  New_Runway
//
//  Created by mack on 2018/3/16.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarTraderChoose.h"
#import "ShopModel.h"
#import "PickCustomView.h"


@interface CarTraderChoose ()<UIGestureRecognizerDelegate,CustomPickViewDelegate>
@property (nonatomic, strong) NSMutableArray *shopArray;

@end

@implementation CarTraderChoose
-(NSMutableArray *)shopArray{
    if (!_shopArray) {
        _shopArray = [[NSMutableArray alloc] init];
    }
    return _shopArray;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self =[ super initWithFrame:frame];
    if (self) {
        [self addSubView];
        
        UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
        tapRecognize.numberOfTapsRequired = 1;
        tapRecognize.delegate = self;
        [tapRecognize setEnabled :YES];
        [tapRecognize delaysTouchesBegan];
        [tapRecognize cancelsTouchesInView];
        
        [self addGestureRecognizer:tapRecognize];
        
    }
    return self;
}

-(void)addSubView{
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.textColor = MAIN_FONT_COLOR();
    titleL.font = [UIFont systemFontOfSize:12];
    [self addSubview:titleL];
    kSelfWeak;
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).mas_offset(14);
        make.centerY.equalTo(weakSelf);
    }];
    self.titleL = titleL;
    
    UITextField *textF = [[UITextField alloc] init];
    textF.placeholder = @"请选择车商";
    textF.tintColor = MAIN_NAV_COLOR();
    textF.textColor = MAIN_FONT_COLOR();
    textF.font = kFontSize(12);
    textF.userInteractionEnabled = NO;
    [self addSubview:textF];
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).mas_offset(50);
        make.right.equalTo(weakSelf).mas_offset(-50);
        make.centerY.equalTo(weakSelf);
    }];
    self.textF = textF;
    
   
    UIImageView *rightImage = [[UIImageView alloc] init];
    rightImage.image = [UIImage imageNamed:@"up"];
    [self addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf).mas_offset(-14);
        make.centerY.equalTo(weakSelf);
    }];
    
}
-(void)handleTap:(UIGestureRecognizer *)recogn{
    
    [self loadShopList];
}
//商铺
-(void)loadShopList{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,shopListApi];
    
    NSDictionary *params = @{
                             @"marketId":[NRWCarUser sharedUser].marketId,
                             @"shopState":@1
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
        ShopModel *model = [ShopModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.shopArray removeAllObjects];
            NSMutableArray *shopNames = [[NSMutableArray alloc] init];
            for (NSDictionary *dic in model.result) {
                shopResult *model = [shopResult mj_objectWithKeyValues:dic];
                [self.shopArray addObject:model];
                [shopNames addObject:model.shopName];
            }
            if (shopNames.count != 0) {
                [self creatChoose:shopNames tag:1];
                
            }else{
                [ShowMessage showHintMessage:@"暂无数据!" duration:2.0 withRemindId:remindFail];

            }
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
}

//单个信息
-(void)creatChoose:(NSMutableArray *)titles tag:(NSInteger)tag{
    
    
    PickCustomView *pickerSingle = [[PickCustomView alloc]init];
    pickerSingle.tag = tag;
    
    pickerSingle.pickerViewHeight = 300;
    [pickerSingle setDataArray:titles];
    [pickerSingle setDefalutSelectRowStr:titles[0]];
    [pickerSingle setDelegate:self];
    [pickerSingle show];
//    [self endEditing:YES];
}
-(void)customPickView:(PickCustomView *)customPickView selectedTitle:(NSString *)selectedTitle index:(NSInteger)index{
    
    self.textF.text = selectedTitle;
    shopResult *model = _shopArray[index];
    self.shopIdBlock(model.shopId, model.shopName);
    
}
@end
